﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Entity
{
    /// <summary>
    /// 用户登录失败实体
    /// </summary>
    public class UserAccessFail
    {
        private UserAccessFail() { }
        public UserAccessFail(User user)
        {           
            Id=Guid.NewGuid();
            this.Myuser = user;            
        }

        public Guid Id { get; init; }
        public Guid Userid { get; init; }

        public User Myuser { get; init; }
        /// <summary>
        /// 失败次数
        /// </summary>
        public int AccessFailedCount { get; private set; }

        private bool lockOut;  //是否锁定
        /// <summary>
        /// 锁定结束时间
        /// </summary>
        public DateTime? LockoutEnd { get; private set; }

        public void Reset()
        {
            lockOut = false;
            LockoutEnd = null;
            AccessFailedCount = 0;
        }

        /// <summary>
        /// 处理一次 登录失败
        /// </summary>
        public void InvokeFail()
        {
            AccessFailedCount++;
            if (AccessFailedCount > 3)
            {
                lockOut = true;
                LockoutEnd = DateTime.Now.AddMinutes(3);
            }
        }

        /// <summary>
        /// 是否锁定
        /// </summary>
        /// <returns></returns>
        public bool IsLockOut()
        {
            if (lockOut) 
            {
                if (LockoutEnd < DateTime.Now) Reset();
                else return true;
            }
            return false;
        }
    }
}
